package org.csu.goodpartner.persistence;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import org.csu.goodpartner.VO.LineItemVO;
import org.csu.goodpartner.VO.OrderVO;
import org.csu.goodpartner.entity.LineItem;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface LineItemMapper extends BaseMapper<LineItem> {
    @Select("select o.orderid, o.userid, o.shopid, o.orderdate, o.addrnumber, o.status, l.itemid, l.quantity, i.quantity as inventory " +
            "from orders o, lineitem l, inventory i " +
            "where o.orderid = l.orderid and o.shopid = i.shopid " +
            "and l.itemid = i.itemid and o.orderid=#{orderId}")
    List<LineItemVO> getLineItemByOrderId(int orderId);

    @Select("select o.orderid, o.userid, o.shopid, o.orderdate, o.addrnumber, o.status, l.itemid, l.quantity, i.quantity as inventory " +
            "from orders o, lineitem l, inventory i " +
            "where o.orderid = l.orderid and o.shopid = i.shopid " +
            "and l.itemid = i.itemid and o.orderid=#{orderId}")
    LineItemVO getLineItem(int orderId);

    @Select("select quantity from lineitem where itemid=#{itemId} and orderid=#{orderId}")
    int getQuantityByItemIdAndOrderId(int itemId, int orderId);

    @Select("select quantity from inventory where itemid=#{itemId} and shopid=#{shopId}")
    int getInventoryByItemIdAndShopId(int itemId, int shopId);

    @Select("select concat(province,city,area,detailAddr) from addr where id = #{addrnum}")
    String getAddrByAddrNum(int addrnum);

    @Select("select phone from addr where id = #{addrnum}")
    String getPhoneByAddrNum(int addrnum);

    @Select("select orders.orderid,orders.userid,orders.status,orders.shopid,a.phone," +
            "concat(a.province,a.city,a.area,a.detailAddr) as addr ,orders.orderdate from orders,addr a " +
            "where orders.addrnumber=a.id and concat(orders.orderid,orders.userid,orders.status,a.phone," +
            "a.province,a.city,a.area,a.detailAddr,orders.orderdate) like '%${like}%' and orders.shopid=#{shopid}")
    List<OrderVO> getOrderVOByLike(String like, int shopid);
}
